package no.ntnu.fp.utils;

import no.ntnu.fp.model.Employee;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Participant {
	private final Employee employee;
	private final Boolean attending;

	public Participant(Employee employee, Boolean attending) {
		this.employee = employee;
		this.attending = attending;
	}
	
	public Participant(Element element) {
		this.employee = new Employee((Element)element.getElementsByTagName("employee").item(0));
		String attending = XmlParser.getTextValue(element, "attending");
		this.attending = (attending == null ? null : Boolean.valueOf(attending));
	}

	public Employee getEmployee() {
		return employee;
	}

	public Boolean isAttending() {
		return attending;
	}
	
	public Element toXml(Document doc) {
		Element participantElement = doc.createElement("participant");
		participantElement.appendChild(employee.toXml(doc));
		participantElement.appendChild(XmlParser.createElementWithValue(
				doc, "attending", (attending == null ? "null" 
													: attending.toString())));
		return participantElement;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((employee == null) ? 0 : employee.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Participant other = (Participant) obj;
		if (employee == null) {
			if (other.employee != null)
				return false;
		} else if (!employee.equals(other.employee))
			return false;
		return true;
	}
	
	
}
